import vue from 'vue'
import VueRouter from 'vue-router'

vue.use(VueRouter)

const Home = () => import('@/views/home/Home')
const Category = () => import('@/views/category/Category')
const Shopcart = () => import('@/views/shopcart/Shopcart')
const Profile = () => import('@/views/profile/Profile')
const GoodsDetail = () => import('@/views/details/GoodsDetail')
// const child = () => import('@/views/profile/ChildComps/child')

const routes = [
	{
		path: '/',
		redirect: '/home'
	},
	{
		path: '/home',
		component: Home
	},
	{
		path: '/category',
		component: Category
	},
	{
		path: '/shopcart',
		component: Shopcart
	},
	{
		path: '/profile',
		component: Profile,
		// children: [{
		// 	path: 'child',
		// 	component: child
		// }]
	},
	{
		path: '/detail',
		component: GoodsDetail,
	}
]

const router = new VueRouter({
	routes,
	mode: 'history'
})

router.onError((error) => {
	const pattern = /Loading chunk (\d)+ failed/g;
	const isChunkLoadFailed = error.message.match(pattern);
	const targetPath = router.history.pending.fullPath;
	if (isChunkLoadFailed) {
		router.replace(targetPath);
	}
});
export default router
